import Vue from 'vue'
import App from './App.vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false,
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false,
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false,
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false,
      },
    ],
  },
  getters: {
    priceAll: (state) =>
      state.goodList.reduce((sum, item) => sum + item.num * item.price, 0),
    checkAll: (state) => state.goodList.every((item) => item.checked),
  },
  mutations: {
    addCount(state, payload) {
      state.goodList[payload].num++
    },
    delCount(state, payload) {
      state.goodList[payload].num--
    },
    delBtn(state, payload) {
      state.goodList.splice(payload, 1)
    },
  },
})

Vue.config.productionTip = false

new Vue({
  store,
  render: (h) => h(App),
}).$mount('#app')
